/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;
import tools.dbconnector;

/**
 *
 * @author cas
 */
public class admin {
    //data semua user yang ada di database
    List<String> users = new ArrayList<String>();
    //data semua kategori yang ada di database
    List<String> kategori = new ArrayList<String>();

    private String username;

    public admin(String username){
        dbconnector db = new dbconnector();
        this.username = username;
        this.users = db.selectUser(this.username);
        this.kategori = db.selectKategori();
    }

    public void addKategori(String nama, String parent, String mod){
        dbconnector db = new dbconnector();
        db.addKategori(nama, parent, mod);
    }

    public void addKategori(String nama, String mod){
        dbconnector db = new dbconnector();
        db.addKategori(nama, mod);
    }

    public void dropKategori(int id){
        dbconnector db = new dbconnector();
        db.dropKategori(id);
    }

    public void updateKategori (int id, String nama, String mod){
        dbconnector db = new dbconnector();
        db.updateKategori(id, nama, mod);
    }

    public void updateKategori(int id, String nama, String parent, String mod){
        dbconnector db = new dbconnector();
        db.updateKategori(id, nama, parent, mod);
    }

    public void updateUser(int maxRoom,String username, int isBan){
        dbconnector db = new dbconnector();
        db.updateUser(maxRoom, username, isBan);
    }

    public void updateUsers(){
        dbconnector db = new dbconnector();
        users.clear();
        users = db.selectUser(this.username);
    }

    public void updateKategori(){
        dbconnector db = new dbconnector();
        kategori.clear();
        kategori = db.selectKategori();
    }

    public void printAllUsers(JspWriter out){
        dbconnector db = new dbconnector();
        updateUsers();
        try {
            if (this.users.isEmpty()) {
                out.println("There is No User in Database");
            } else {
                for (int i = 0; i < users.size(); i++) {
                    String[] user = db.getData(users.get(i));
                    String[] ban = new String[2];
                    if (user[3].equals("1")){
                        ban[0] = "ban";
                        ban[1] = "unban";
                    } else {
                        ban[1] = "ban";
                        ban[0] = "unban";
                    }
                    out.println("<div class='single_user'>" +
                                    "<form action='functions/process.jsp?task=updateuser' method='post'>" +
                                        "<div class='nama_users'>" + user[0] + "</div>" +
                                        "<div class='email_users'>" + user[1] + "</div>" +
                                        "<div class='maxroom_users'><input type='text' class='maxroom' name='maxroom' value='" + user[2] + "' /></div>" +
                                        "<div class='view_ban'><select name='ban_option' class='ban_option'><option value='" + ban[0] + "'>" + ban[0] + "</option><option value='" + ban[1] + "'>" + ban[1] + "</option></select></div>" +
                                        "<div class='but'><input type='submit' class='button' value=''/>" +
                                        "<input type='hidden' name='uname' id='uname' value='" + users.get(i) + "'></div>" +
                                        "<div class='clear'></div>" +
                                    "</form>" +
                                "</div>");
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void printAllKategori(JspWriter out){
        dbconnector db = new dbconnector();
        updateKategori();
        String[] nama = new String[users.size()];
        try {
            if (this.kategori.isEmpty()) {
                out.println("There is No Kategori in Database");
            } else {
                for(int j = 0; j < users.size(); ++j){
                    nama[j] = users.get(j);
                }
                for (int i = 0; i < kategori.size(); i++){
                    String[] kat = db.getDataKategori(Integer.parseInt(kategori.get(i)));
                    List<String> subkategori = db.selectSubKategori(kat[0]);
                    
                    out.println("<div id='"+kategori.get(i)+"'>" +
                                    "<div class='single_kategori'>" +
                                        "<div class='change'>" +
                                        "<form action='functions/process.jsp?task=changekategori' method='post'>");
                                            out.println("<div class='expand' onclick=\"expanding(\'"+kat[0]+"\')\"></div>");
                                            out.println("<div class='nama_kat'><input type='text' class='namakat' name='namakat' value='" + kat[0] + "' /></div>" +
                                            "<div class='nama_mod'>" +
                                                "<select name='mod_option' class='mod_option'>" +
                                                "<option value='" + kat[2] + "'>" + kat[2] + "</option>");
                                                for (int j = 0; j < nama.length; ++j){
                                                    out.println("<option value='" + nama[j] + "'>" + nama[j] + "</option>");
                                                }
                                            out.println("</select></div>" +
                                            "<input type='submit' class='button' value=''>" +
                                            "<input type='hidden' name='uname' id='uname' value='" + kategori.get(i) +"'>" +
                                            "<input type='hidden' name='parent' id='parent' value='" + kat[0] +"'>" +
                                        "</form>" +
                                        "</div>" +
                                        "<div class='drop'>");
                                            if (subkategori.isEmpty()){
                                                out.println("<form action='functions/process.jsp?task=dropkategori' method='post'>" +
                                                    "<input type='submit' class='button1' value=''>" +
                                                    "<input type='hidden' name='uname' id='uname' value='" + kategori.get(i) + "'>" +
                                                "</form>");
                                            }
                                        out.println("</div>" +
                                "</div></div>");
                    
                    if (!subkategori.isEmpty()){
                        for (int k = 0; k < subkategori.size(); ++k){
                            String[] subkat = db.getDataKategori(Integer.parseInt(subkategori.get(k)));

                            out.println("<div id='"+kat[0]+"' name='"+kat[0]+"' style='display: none'>" +
                                            "<div class='single_kategori'>" +
                                                "<div class='change'>" +
                                                "<form action='functions/process.jsp?task=changekategori' method='post'>");
                                                    out.println("<div class='expand2'></div>");
                                                    out.println("<div class='nama_kat'><input type='text' class='namakat' name='namakat' value='" + subkat[0] + "' /></div>" +
                                                    "<div class='nama_mod'>" +
                                                        "<select name='mod_option' class='mod_option'>" +
                                                        "<option value='" + subkat[2] + "'>" + subkat[2] + "</option>");
                                                        for (int j = 0; j < nama.length; ++j){
                                                            out.println("<option value='" + nama[j] + "'>" + nama[j] + "</option>");
                                                        }
                                                    out.println("</select></div>" +
                                                    "<input type='submit' class='button' value=''>" +
                                                    "<input type='hidden' name='uname' id='uname' value='" + subkategori.get(k) +"'>" +
                                                "</form>" +
                                                "</div>" +
                                                "<div class='drop'>" +
                                                    "<form action='functions/process.jsp?task=dropkategori' method='post'>" +
                                                        "<input type='submit' class='button1' value=''>" +
                                                        "<input type='hidden' name='uname' id='uname' value='" + subkategori.get(k) + "'>" +
                                                    "</form>" +
                                                "</div>" +
                                        "</div></div>");
                        }
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void PrintAddKategori (JspWriter out){
        dbconnector db = new dbconnector();
        updateKategori();
        String[] kat = new String[kategori.size()];
        updateUsers();
        String[] nama = new String[users.size()];
        try {
            out.println("<form action='functions/process.jsp?task=addkategori' method='post'>" +
                            "<div id='kat_baru'><input type='text' name='kat_baru' size='25' /></div>");
            if (!this.kategori.isEmpty()) {
                for(int j = 0; j < kategori.size(); ++j){
                    String[] temp = db.getDataKategori(Integer.parseInt(kategori.get(j)));
                    kat[j] = temp[0];
                }
                for(int j = 0; j < users.size(); ++j){
                    nama[j] = users.get(j);
                }
                out.println("<div id='parent_baru'><select name='kat_baru_option' class='kat_option'>" +
                            "<option value='null'>NULL</option>");
                            for (int j = 0; j < kat.length; ++j){
                                out.println("<option value='" + kat[j] + "'>" + kat[j] + "</option>");
                            }
                out.println("</select></div>");
            }
            if (!this.users.isEmpty()){
                for(int j = 0; j < users.size(); ++j){
                    nama[j] = users.get(j);
                }
                out.println("<div id='mod_baru'><select name='mod_baru_option' class='mod_option'>");
                            for (int j = 0; j < nama.length; ++j){
                                out.println("<option value='" + nama[j] + "'>" + nama[j] + "</option>");
                            }
                out.println("</select></div>");
            }
            out.println("<input type='submit' class='button2' value=''/></form>");
        } catch (IOException ex) {
            Logger.getLogger(admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
